#include "nvs_driver.h"

esp_err_t custom_nvs_part_init(const char *label);
esp_err_t custom_nvs_part_write_str(const char *label,const char *namespace,const  char *str_key,const char *str_val);
esp_err_t custom_nvs_part_read_str(const char *label,const char *namespace,const  char *str_key, char *str_get_val,size_t *str_val_len);
esp_err_t custom_nvs_part_write_u8(const char *label,const char *namespace,const char *str_key,u8 *u8_val);
esp_err_t custom_nvs_part_read_u8(const char *label,const char *namespace,const char *str_key,u8 *u8_val);


u8 driver_nvs_init()
{          
    esp_err_t ret;
    ret = nvs_flash_init();             /* 初始化NVS */
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
    {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    return ret;
}

//初始化
esp_err_t driver_nvs_part_init()
{
    esp_err_t ret =ESP_FAIL;
    ret = custom_nvs_part_init(CUSTOM_NVS_PART_LABEL);
    
    esp_err_to_name(ret);
    return ret;
}


//字符串保存函数
esp_err_t driver_nvs_part_write_str(const char *str_key,const char *str_val)
{
    esp_err_t ret =ESP_FAIL;
    ret = custom_nvs_part_write_str(CUSTOM_NVS_PART_LABEL,CUSTOM_NVS_PART_NAMESPACE,str_key,str_val);
    return ret;
}


//字符串读取函数
esp_err_t driver_nvs_part_read_str(const char *str_key,char *str_get_val,size_t *str_val_len)
{
    esp_err_t ret =ESP_FAIL;
    ret = custom_nvs_part_read_str(CUSTOM_NVS_PART_LABEL,CUSTOM_NVS_PART_NAMESPACE,str_key,str_get_val,str_val_len);
    return ret;
}
//u8写入函数
esp_err_t driver_nvs_part_write_u8(const char *str_key,u8 *u8_val)
{
    esp_err_t ret =ESP_FAIL;
    ret = custom_nvs_part_write_u8(CUSTOM_NVS_PART_LABEL,CUSTOM_NVS_PART_NAMESPACE,str_key,u8_val);
    printf("ret %d\n",ret);
    return ret;
}

//u8读取函数
esp_err_t driver_nvs_part_read_u8(const char *str_key,u8 *u8_val)
{
    esp_err_t ret =ESP_FAIL;
    ret = custom_nvs_part_read_u8(CUSTOM_NVS_PART_LABEL,CUSTOM_NVS_PART_NAMESPACE,str_key,u8_val);
    LOG_INFO("RET2 %d\n",ret);
    return ret;
}


esp_err_t custom_nvs_part_init(const char *label)
{
    esp_err_t ret =ESP_FAIL;
    ret = nvs_flash_init_partition(label);
    
    return ret;
}

esp_err_t custom_nvs_part_write_str(const char *label,const char *namespace,const char *str_key,const char *str_val)
{
    nvs_handle_t my_handle;
    esp_err_t err = ESP_FAIL;
    //open 
    err = nvs_open(namespace, NVS_READWRITE, &my_handle);
    //write
    err = nvs_set_str(my_handle,str_key,str_val);
    //save
    err = nvs_commit(my_handle);
    //close
    nvs_close(my_handle);

    return err;
}











esp_err_t custom_nvs_part_read_str(const char *label,const char *namespace,const char *str_key, char *str_get_val,size_t *str_val_len)
{
    nvs_handle_t my_handle;
    esp_err_t err = ESP_FAIL;
    *str_val_len =0;//清零
    memset(str_get_val,0,100);
    //open
    err = nvs_open(namespace, NVS_READWRITE, &my_handle);
    //read
    err = nvs_get_str(my_handle,str_key,NULL,str_val_len); //获取字符串的长度
    err = nvs_get_str(my_handle,str_key,str_get_val,str_val_len); //获取具体的字符串内容
    nvs_close(my_handle);
    return err;

}






esp_err_t custom_nvs_part_write_u8(const char *label,const char *namespace,const char *str_key,u8 *u8_val)
{
    nvs_handle_t my_handle;
    esp_err_t err = ESP_FAIL;
    //open 
    err = nvs_open(namespace, NVS_READWRITE, &my_handle);
    //write
    err = nvs_set_u8(my_handle,str_key,*u8_val);
    //save
    err = nvs_commit(my_handle);
    //close
    nvs_close(my_handle);

    return err;
}


esp_err_t custom_nvs_part_read_u8(const char *label,const char *namespace,const char *str_key,u8 *u8_val)
{
    nvs_handle_t my_handle;
    esp_err_t err = ESP_FAIL;

    //open
    err = nvs_open(namespace, NVS_READWRITE, &my_handle);
    //read
    err = nvs_get_u8(my_handle,str_key,u8_val); //获取字符串的长度

    nvs_close(my_handle);
    return err;

}